package com.chenjf.controller;

import com.chenjf.config.EmpsConfig;
import com.chenjf.feign.DepartsFeign;
import com.chenjf.po.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;

@RequestMapping("emps")
@RestController
public class EmpController {

    //服务之间通信（1,不带参数）
    @GetMapping
    public String pingDeparts() {
        RestTemplate restTemplate = new RestTemplate();
        String forObject = restTemplate.getForObject("http://localhost:9000/departs/list", String.class);
        System.out.println("forObject = " + forObject);
        return "departs ok!";
    }

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    //服务之间通信（2,不带参数）
    @GetMapping("pingDeparts2")
    public String pingDeparts2() {
        RestTemplate restTemplate = new RestTemplate();
        ServiceInstance departs = loadBalancerClient.choose("DEPARTS");
        URI uri = departs.getUri();
        System.out.println("uri = " + uri);
        String forObject = restTemplate.getForObject(uri + "/departs/list", String.class);
        System.out.println("forObject = " + forObject);
        return "departs ok!";
    }

    @Autowired
    private EmpsConfig empsConfig;

    //服务之间通信（3,不带参数）
    @GetMapping("pingDeparts3")
    public String pingDeparts3() {
        String forObject = empsConfig.loadBalanced().getForObject("http://DEPARTS/departs/list", String.class);
        System.out.println("forObject = " + forObject);
        return "departs ok!";
    }

    @Autowired
    private DepartsFeign departsFeign;

    @GetMapping("pingDeparts4")
    public String pingDeparts4() {
        String list = departsFeign.list();
        System.out.println("list = " + list);
        return "departs ok!";
    }

    @GetMapping("pingDeparts5")
    public String pingDeparts5() {
        String list = departsFeign.list1(1, 2);
        System.out.println("list = " + list);
        return "departs ok!";
    }

    @GetMapping("pingDeparts6")
    public String pingDeparts6() {
        String list = departsFeign.list2(2, 3);
        System.out.println("list = " + list);
        return "departs ok!";
    }

    @GetMapping("pingDeparts7")
    public String pingDeparts7() {
        String list = departsFeign.list3(new Page(3, 4));
        System.out.println("list = " + list);
        return "departs ok!";
    }

}
